package com.zhuss.meeting.processor;

import com.zhuss.bean.bo.meeting.room.ConfirmTimeBo;
import com.zhuss.bean.bo.meeting.room.GetSituationBo;
import com.zhuss.bean.unit.meeting.ApplyTime;
import com.zhuss.bean.vo.meeting.apply.ListSituationVo;
import com.zhuss.bean.vo.meeting.time.ListRoomTimeVo;
import com.zhuss.enums.MeetRoomCheck;
import com.zhuss.meeting.db.dao.ext.MeetingApplyDao;
import com.zhuss.meeting.db.dao.ext.MeetingRoomTimeDao;
import com.zhuss.meeting.db.entity.MeetingRoomApply;
import com.zhuss.meeting.db.entity.MeetingRoomTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhushanshuo
 * @Date: 2020/2/28 15:35
 */
@Component("timeRead")
public class TimeRead {

    @Autowired
    private MeetingRoomTimeDao meetingRoomTimeDao;

    @Autowired
    private MeetingApplyDao meetingApplyDao;

    public void confirm(ConfirmTimeBo bo){
        MeetingRoomTime m = new MeetingRoomTime();
        m.setApplyId(bo.getApplyId());
        m.setRoomId(bo.getRoomId());
        m.setRoomName(bo.getRoomName());
        m.setMeetingStartTime(bo.getMeetingStartTime());
        m.setMeetingEndTime(bo.getMeetingEndTime());
        m.setArea(bo.getArea());

        m.setCreator(bo.getConcurrentUserName());
        m.setCreatorId(bo.getConcurrentUserId());
        m.setCreateTime(new Date());
        meetingRoomTimeDao.create(m);

        MeetingRoomApply apply = meetingApplyDao.applyDetail(bo.getApplyId());
        apply.setRoomId(m.getRoomId());
        apply.setRoomName(m.getRoomName());
        apply.setStatus(MeetRoomCheck.已审核.key());
        meetingApplyDao.updateByPrimaryKeySelective(apply);

    }

    public Object getSituation(GetSituationBo bo) {
        List<ListSituationVo> result = new ArrayList<>();
        List<ListRoomTimeVo> listRoomTime = meetingRoomTimeDao.listSituation(bo);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (ListRoomTimeVo list : listRoomTime){
            ListSituationVo vo = new ListSituationVo();
            List<ApplyTime> time = new ArrayList<>();
            ApplyTime applyTime = new ApplyTime();
            String startTime = formatter.format(list.getMeetingStartTime());
            String endTime = formatter.format(list.getMeetingEndTime());
            applyTime.setStratTime(startTime);
            applyTime.setEndTime(endTime);
            time.add(applyTime);
            vo.setTime(time);
            vo.setRoomName(list.getRoomName());
            result.add(vo);
        }
        return result;
    }
}
